package com.jiesen.base.modules.login.jsLoginUser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiesen.base.baseDao.BaseServiceImpl;
import com.jiesen.base.modules.company.model.JsPublicUsersRecord;
import com.jiesen.base.modules.login.jsLoginUser.mapper.JsLoginUserDao;
import com.jiesen.base.modules.login.jsLoginUser.model.JsLoginUser;
import com.jiesen.base.modules.login.jsRole.model.JsRole;
import com.jiesen.base.modules.login.jsLoginUser.service.JsLoginUserService;
import com.jiesen.base.modules.login.jsRole.service.JsRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.List;

@Service
public class JsLoginUserServiceImpl extends BaseServiceImpl<JsLoginUserDao, JsLoginUser> implements JsLoginUserService {

    @Autowired
    private JsRoleService jsRoleService;



    //提供前端登录的方法
    @Override
    public JsLoginUser findUserByS(String s) {
        QueryWrapper<JsLoginUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email",s);
        queryWrapper.or();
        queryWrapper.eq("telephone",s);
        queryWrapper.or();
        queryWrapper.eq("account",s);
        List<JsLoginUser> jsLoginUsers = baseMapper.selectList(queryWrapper);
        if(CollectionUtils.isEmpty(jsLoginUsers)){
            return null;
        }
        JsLoginUser jsLoginUser = jsLoginUsers.get(0);

        //查询用户角色信息
        List<JsRole> roles = jsRoleService.findRolePByUserId(jsLoginUser.getId());
        for (JsRole role : roles) {
            role.setBranchId(jsLoginUser.getBranchId());
        }
        jsLoginUser.setJsRoles(roles);

        return jsLoginUser;
    }

    //提供给shiro用户权限检测的方法
    @Override
    public JsLoginUser findUserRoleById(Integer id) {
        //查询用户信息
        JsLoginUser jsLoginUser = baseMapper.selectById(id);
        if(jsLoginUser==null){
            return null;
        }

        //查询用户角色信息
        List<JsRole> roles = jsRoleService.findRolePByUserId(id);
        jsLoginUser.setJsRoles(roles);

        return jsLoginUser;
    }

    //查询所有用户
    @Override
    public List<JsLoginUser> findAll() {
        return baseMapper.selectList(null);
    }

    //根据id查询用户信息与角色信息
    @Override
    public JsLoginUser findRoleByUserId(Integer id) {
        JsLoginUser jsLoginUser = baseMapper.selectById(id);
        if(jsLoginUser == null){
            return null;
        }
        //查询用户角色信息
        List<JsRole> roles = jsRoleService.findRoleByUserId(id);
        jsLoginUser.setJsRoles(roles);

        return jsLoginUser;
    }

    //修改用户的角色信息
    @Override
    public boolean updataUserRole(Integer uid, List<Integer> rids) {
        baseMapper.deleteRoleByUserId(uid);
        for (Integer rid : rids) {
            baseMapper.insertRoleByUserId(uid,rid);
        }
        return true;
    }



    //删除用户同时删除关联表
    @Override
    @Transactional
    public boolean removeById(Serializable id) {
        baseMapper.deleteRoleByUserId(id);
        return super.removeById(id);
    }
}
